package com.lyl.async.controller;

import cn.hutool.core.date.DateUtil;
import com.lyl.async.service.OrderService;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class AsyncController {

    @Resource
    private OrderService orderService;
    @Resource
    private ThreadPoolTaskExecutor localTaskExecutor;


    @GetMapping("/sync")
    public String sync(){
        orderService.getAddress();
        return "ok : " + DateUtil.now();
    }

    @GetMapping("/sync2")
    public String sync2() throws ExecutionException, InterruptedException {
        Future<String> age = orderService.getAge();
        return "ok : " + DateUtil.now() + " 年龄: " + age.get();
    }

    @GetMapping("/sync3")
    public String sync3() throws ExecutionException, InterruptedException {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(orderService::getName, localTaskExecutor);
        CompletableFuture.allOf(future);
        return "ok : " + DateUtil.now() + " name = " + future.get();
    }




}
